package com.ljsw.util;

import java.io.File;
import java.io.FileInputStream;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;

public class MD5 {
	public String getMd5(String path) {
		try {
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			File file = new File(path);
			FileInputStream in = new FileInputStream(file);
			FileChannel ch = in.getChannel();
			MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY,
					0, file.length());
			md5.update(byteBuffer);

			byte[] tmp = md5.digest();
			StringBuilder sb = new StringBuilder();
			for (byte b : tmp) {
				sb.append(String.format("%02X", b));
			}
			// md5
			String stringSb = sb.toString().toLowerCase();
			System.out.println("sb:" + stringSb);
			return stringSb;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;

	}

	public static void main(String[] args) {
		System.out.println(new MD5().getMd5("C:\\Users\\admin\\Desktop\\20151209015426.jpg"));
	}

}
